package drrr.controller;

import com.opensymphony.xwork2.ActionContext;
import drrr.orm.RoomEntity;
import drrr.orm.RoomMemberEntity;
import org.apache.struts2.ServletActionContext;

import java.util.Map;

/**
 * Created by lirance on 15/5/24.
 * 销毁房间
 */
public class DeleteRoom extends BaseAction {

    @Override
    public String execute() throws Exception {
        Map<String, Object> session = ServletActionContext.getContext().getSession();
        RoomEntity roomEntity = (RoomEntity) session.get("room");
        RoomMemberEntity roomMemberEntity = (RoomMemberEntity) session.get("member");
        //获取房间人数
        String roomOnline=String.format("roomOnline%s", roomEntity.getId() + "");
        int onlineNum = (int) ActionContext.getContext().getApplication().get(roomOnline);

         if (onlineNum > 1) { //房间人数为一,删除当前用户，删除房间历史记录，销毁房间，
             return ERROR;
        } else{
             getAllService().getRoomService().execute("DELETE FROM roomMember WHERE (memberId=" + roomMemberEntity.getMemberId()+" AND roomId="+ roomMemberEntity.getRoomId()+")");
             getAllService().getRoomService().execute("DELETE FROM roomChatList WHERE roomid=" + roomEntity.getId());
             getAllService().getRoomService().execute("DELETE FROM room WHERE id=" + roomEntity.getId());
             return SUCCESS;
         }

    }
}
